package com.prj.ufdm.cloud.hystrix;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableCircuitBreaker
@RestController
public class EurekaHystrixApplication implements EnvironmentAware {

	private static Environment env;
	
	@Autowired
	private MyService myService;
	
	@Override
	public void setEnvironment(Environment environment) {
		env = environment;
	}
	
	@RequestMapping("/")
	public String ok() {
		return this.myService.ok();
	}

	@RequestMapping("/fail")
	public String fail(@RequestHeader(name = "X-No-Fail", required = false) String noFail) {
		
		System.out.println("\n noFail:"+noFail);
		
		
		if (noFail != null) {
			return this.myService.fail(false);
		}
		return this.myService.fail(true);
	}
	
	public static void main(String[] args) {
		
		SpringApplication.run(EurekaHystrixApplication.class, args);
        
        System.out.println("\n Eureka Hystrix "+ env.getProperty("server.port") +" 启动成功................");
    }
	


}
